﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using A2DFramework.XmlService;

namespace A2DFramework.EventService.SuperSocket.Core
{
    public static class Helper
    {
        public static bool IsEventBusCommand_Publish(string msg, out string evtClassPath, out string evtXml)
        {
            evtClassPath = "";
            evtXml = "";
            string[] ms = msg.Split(' ');
            if (ms == null || ms.Count() <= 1)
                return false;
            if (ms[0] == "ME" || ms[0] == "Publish" || ms[0] == "Subscribe")
            {
                evtClassPath = RestoreSpecialCharacters(ms[1]);
                evtXml = RestoreSpecialCharacters(ms[2]);
                return true;
            }
            return false;
        }
        public static string GeneratePublishCommand<T>(T o)
        {
            return GenerateCommand("Publish", o.GetType().FullName, SerializeUtils.Serialize2XML<T>(o));
        }
        public static string GeneratePublishCommand(object o)
        {
            return GenerateCommand("Publish", o.GetType().FullName, SerializeUtils.Serialize2XML(o.GetType(), o));
        }

        public static string GenerateSubscribeCommand<T>()
        {
            return GenerateCommand("Subscribe", typeof(T).FullName);
        }
        public static string GenerateSubscribeCommand(Type type)
        {
            return GenerateCommand("Subscribe", type.FullName);
        }
        public static string GenerateSubscribeCommand(string typeFullName)
        {
            return GenerateCommand("Subscribe", typeFullName);
        }
        public static string GenerateDeclareCommand(string serverIdentity)
        {
            return GenerateCommand("ME", serverIdentity);
        }
        public static string GenerateCommand(string commandType, params string[] ps)
        {
            string cmd = string.Empty;

            List<string> lst = new List<string>();
            lst.Add(ReplaceSpecialCharacters(commandType));
            foreach (string s in ps)
                lst.Add(ReplaceSpecialCharacters(s));
            lst.ForEach(t => cmd += t + " ");

            cmd = cmd.Trim();

            return cmd;
        }
        public static string ReplaceSpecialCharacters(string src)
        {
            src = src.Replace("<", "*XIAOYUHAO*");
            src = src.Replace(">", "*DAYUHAO*");
            src = src.Replace("'", "*DANYINHAO*");
            src = src.Replace("\"", "*SHUANGYINHAO*");
            src = src.Replace(" ", "*KONGGE*");
            src = src.Replace("\r\n", "*HUICHE*");
            return src;
        }

        public static string RestoreSpecialCharacters(string src)
        {
            src = src.Replace("*XIAOYUHAO*", "<");
            src = src.Replace("*DAYUHAO*", ">");
            src = src.Replace("*DANYINHAO*", "'");
            src = src.Replace("*SHUANGYINHAO*", "\"");
            src = src.Replace("*KONGGE*", " ");
            src = src.Replace("*HUICHE*", "\r\n");
            return src;
        }
    }
}
